<?php
/**
 *	Home Actions.
 *	@package		mv2.action
 *	@extends		CMF_Krypton_Core_Action
 *	@uses			Logic_Role
 *	@uses			Logic_Right
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			29.08.2007
 *	@version		0.1
 */
/**
 *	Home Actions.
 *	@package		mv2.action
 *	@extends		CMF_Krypton_Core_Action
 *	@uses			Logic_Role
 *	@uses			Logic_Right
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			29.08.2007
 *	@version		0.1
 */
class Action_Administration_Role extends CMF_Krypton_Core_DefinitionAction
{
	/**
	 *	Constructor.
	 *	@access		public
	 *	@return		void
	 */
	function __construct()
	{
		$request = CMF_Krypton_Core_Registry::getStatic('request');
		parent::__construct();
		$this->loadLanguage( 'administration.role','role' );
        $this->addAction( 'addRole' );
        $this->addAction( 'editRole' );
        $this->addAction( 'removeRole' );
        $this->addAction( 'setPages' );
        $this->addAction( 'setRights' );
	}

	/**
	 *	Adds a Role.
	 *	@access		protected
	 *	@return		void
	 */
    protected function addRole()
    {
		$request	= $this->registry->get( 'request' );
		$data		= $request->getAll();
		$logic		= new Logic_Role();
		try
		{
			$roleId	= $logic->addRole( $data, 'administration/role', 'addRole', 'add_' );
			$this->messenger->noteSuccess( $this->words['role']['msg']['success_role_added'] );
			$request->remove( 'add' );
			$request->remove( 'add_title' );
			$request->remove( 'add_description' );
			$request->set( 'roleId', $roleId );
		}
		catch( Exception $e )
		{
			$this->handleException( $e, 'role', 'addRole' );
		}
    }

	/**
	 *	Edits a Role.
	 *	@access		protected
	 *	@return		void
	 */
    protected function editRole()
    {
		$request	= $this->registry->get( 'request' );
		$data		= $request->getAll();
		$roleId		= $request->get( 'roleId' );
		$logic		= new Logic_Role();
		try
		{
			if( $logic->editRole( $roleId, $data, 'administration/role', 'editRole', 'edit_' ) )
				$this->messenger->noteSuccess( $this->words['role']['msg']['success_role_edited'] );
			else
				$this->messenger->noteError( $this->words['role']['msg']['error_role_not_edited'] );
			$request->remove( 'roleId' );
		}
		catch( Exception $e )
		{
			$this->handleException( $e, 'role', 'editRole' );
		}
    }

	/**
	 *	Removes a Role and its Role Rights.
	 *	@access		protected
	 *	@return		void
	 */
    protected function removeRole()
    {
		$request		= $this->registry->get( 'request' );
		$roleId			= $request->get( 'roleId' );

		try
		{
			if( Logic_Role::removeRole( $roleId ) )
			{
				$this->messenger->noteSuccess( $this->words['role']['msg']['success_role_removed'] );
				$request->remove( 'roleId' );
			}
			else
				$this->messenger->noteError( $this->words['role']['msg']['error_role_not_removed'] );
		}
		catch( Exception $e )
		{
			$this->handleException( $e, 'role', 'editRole' );
		}
    }

	protected function setPages()
	{
		$request	= $this->registry->get( 'request' );
		$controller	= $this->registry->get( 'controller' );

		$setPages	= $request->get( 'links' );
		$roleId		= $request->get( 'roleId' );
		$role		= strtolower( str_replace( " ", "_", Logic_Role::getRoleTitle( $roleId ) ) );

		$added		= 0;
		$removed	= 0;
		$allPages	= $controller->getPages( 'main' );
		foreach( $allPages as $pageId => $pageData )
		{
			if( in_array( $pageId, $setPages ) )
			{
				if( !in_array( $role, $pageData['roles'] ) )
					$added	+= (int) Logic_Authentication::addRoleToPage( $role, $pageId );
			}
			else
			{
				if( in_array( $role, $pageData['roles'] ) )
					$removed	+= (int) Logic_Authentication::removeRoleFromPage( $role, $pageId );
			}
		}
		if( $added )
			$this->messenger->noteSuccess( $this->words['role']['msg']['success_page_rights_added'], $added );
		if( $removed )
			$this->messenger->noteSuccess( $this->words['role']['msg']['success_page_rights_removed'], $removed );
	}
}
?>